/*
 * BASocketWrapper.java
 * classes : com.qim.basdk.network.BASocketWrapper
 * @author xuwei
 * V 1.0.0
 * Created at 2015年8月20日 上午10:39:57
 */
package com.langsheng.lsintell.network;

import com.langsheng.lsintell.utils.LSConst;
import com.langsheng.lsintell.utils.LSLog;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * com.qim.basdk.network.LSSocketWrapper
 *
 * socket封装实现
 *
 * @author xuwei
 *
 *         created at 2015年8月20日 上午10:39:57
 */
public class LSSocketWrapper {
    public final static String TAG = "LSSocketWrapper";

    private Socket socket = null;
    private OutputStream outputStream = null;
    private InputStream inputStream = null;

    // socket连接状态
    private int connectStatus = LSConst.SOCKET_UNCONNECTED;

    /**
     * 连接服务器
     *
     * @param host
     * @param port
     * @param timeOut
     * @return
     */
    public synchronized int connect(String host, int port, int timeOut) {
        if (connectStatus == LSConst.SOCKET_CONNECTING) {
            return connectStatus;
        }

        try {
            if (connectStatus == LSConst.SOCKET_CONNECTED) {
                socket.close();
                socket = null;
            }

            connectStatus = LSConst.SOCKET_CONNECTING;
            socket = new Socket();
            LSLog.e(TAG, "address : " + host + ":" + port);
            socket.connect(new InetSocketAddress(host, port), timeOut);
            inputStream = socket.getInputStream();
            outputStream = socket.getOutputStream();
            connectStatus = socket.isConnected() ? LSConst.SOCKET_CONNECTED : LSConst.SOCKET_UNCONNECTED;
        } catch (Exception e) {
            LSLog.e(TAG, "", e);
            connectStatus = LSConst.SOCKET_UNCONNECTED;
        }

        return connectStatus;
    }

    /**
     * 往socket端口里写入数据
     *
     * @param bytes
     * @return
     * @throws Exception
     */
    public int write(byte[] bytes) throws Exception {
        int written = 0;
        if (connectStatus == LSConst.SOCKET_CONNECTED && outputStream != null) {
            outputStream.write(bytes);
            outputStream.flush();
            written = bytes.length;
        }
        return written;
    }

    /**
     * 从socket端口读取数据
     *
     * @param bytes
     * @return
     * @throws Exception
     */
    public int read(byte[] bytes) throws Exception {
        int read = 0;
        if (connectStatus == LSConst.SOCKET_CONNECTED && inputStream != null) {
            read = inputStream.read(bytes);
        }
        return read;
    }

    /**
     * 检查socket连接状态
     *
     * @return
     */
    public synchronized boolean isConnected() {
        return connectStatus == LSConst.SOCKET_CONNECTED;
    }

    /**
     * 关闭socket连接
     */
    public void close() {
        try {
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (Exception e) {
            LSLog.e(TAG, "", e);
        } finally {
            outputStream = null;
        }

        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            inputStream = null;
        }

        try {
            if (socket != null) {
                socket.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            socket = null;
        }
        connectStatus = LSConst.SOCKET_UNCONNECTED;
    }

}
